#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    ui->textEdit->setVisible(false);
    this->setWindowTitle("UseLess Games");
    users = new user();
    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(co_SQL()));
}

void Login::co_SQL()
{
    QSqlDatabase sqldb = QSqlDatabase::addDatabase("QMYSQL");
    sqldb.setHostName("localhost");
    sqldb.setDatabaseName("uselessdb");
    sqldb.setUserName("root");
    sqldb.setPassword("");
    ui->textEdit->setPlainText("");
    ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Connected to SQL : \n");
    if (!sqldb.open())
    {
        QMessageBox::critical(this, "Error connection", "Unable to connect to SQL Server.");
        ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Error connection... Check your connection to Internet");
    }
    else
    {
        QSqlQuery requete;
        requete.exec("SELECT * FROM user");
        int pro(0);
        while (requete.next())
        {
            pro++;
            QMessageBox::warning(this, "test", requete.value("pseudo").toString());
            if (requete.value("pseudo").toString() == ui->lineEdit->text())
            {
                QMessageBox::warning(this, "test", requete.value("pseudo").toString());
                ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Pseudo found ...\n");
                ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Check Password ...\n");
                QCryptographicHash cryptmd(QCryptographicHash::Md5);
                QCryptographicHash cryptsh(QCryptographicHash::Sha1);
                QString password = ui->lineEdit_2->text();
                cryptmd.addData(password.toUtf8());
                cryptsh.addData(cryptmd.result());
                if (requete.value("password").toByteArray() == cryptsh.result())
                {
                    QMessageBox::warning(this, "test", requete.value("pseudo").toString());
                    if (requete.value("ban").toBool() == false)
                    {
                        QMessageBox::warning(this, "test", requete.value("pseudo").toString());
                        QVector<QString > games;
                        ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Password correct.\n");
                        QString game = requete.value("games").toString();
                        games.push_back("");
                        for (int i = 0; i < game.size(); i++)
                        {
                            if (game[i] == ';')
                            {
                                games.push_back("");
                                i++;
                            }
                            games[games.size() - 1] += game[i];
                        }
                        QMessageBox::warning(this, "test", requete.value("pseudo").toString());
                        ui->textEdit->setPlainText(ui->textEdit->toPlainText() + "Games Founds.\n");
                        users->setParam(requete.value("pseudo").toString(), requete.value("pseudo_visible").toString(), requete.value("mail").toString()
                                       ,requete.value("dev").toBool(), requete.value("id").toInt());
                        users->setGameList(games);
                    }
                    else
                        QMessageBox::warning(this, "User type", "Vous avez été banni du serveur. Veuillez contacter un administrateur.");
                }
            }
        }
    }
}

Login::~Login()
{
    delete ui;
}

void Login::on_pushButton_2_clicked()
{
    ui->pushButton_2->setVisible(false);
    ui->textEdit->setVisible(true);
}

void Login::on_pushButton_3_clicked()
{
    this->setEnabled(false);
    Inscription *w = new Inscription();
    w->show();
    this->setEnabled(true);
}
